package com.yxjx.duoxue.customview;

import java.util.ArrayList;

import com.yxjx.duoxue.R;
import com.yxjx.duoxue.YxTearchersHSVHelper;
import com.yxjx.duoxue.YxTearchersHSVHelper.HsvAdapter;
import com.yxjx.duoxue.course.TeacherIntroductionActivity;
import com.yxjx.duoxue.datamodel.DMTeacher;
import com.yxjx.duoxue.network.NwUtils;
import com.yxjx.duoxue.utils.UIHelper;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
/**
 * This class is used to  show common used "Image/Text mixed" data which is embed in ScrollView.
 * @author mengmengchen
 *
 */
public class YxDetailedInfoView extends YxListView {
	
	public static final int TYPE_IMG = 0;
	public static final int TYPE_TEXT = 1;
	public static final int TYPE_TEACHER_ITEM = 2;
	public static final int TYPE_HORIZONTAL_SCROLL_TEACHERS = 3;
	public static final int TYPE_COUNT = 4;
	
	public YxDetailedInfoView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				YxListAdapter adpater = (YxListAdapter) getAdapter();
				Context context = arg0.getContext();
				if (adpater.getItemViewType(arg2) == TYPE_TEACHER_ITEM) {
					DMTeacher teacher = (DMTeacher) adpater.getDatas().get(arg2);
					Intent intent = new Intent(context, TeacherIntroductionActivity.class);
					intent.putExtra(NwUtils.KEY_ID, "" + teacher.getId());
					context.startActivity(intent);
				}
			}
		});
		
	}
	
	@Override
	public void setAdapter(ListAdapter listadapter) {
		YxListAdapter adapter;
		if (listadapter instanceof YxListAdapter) {
			adapter = (YxListAdapter) listadapter;
		} else {
			return;
		}
		
		if (adapter.getCount() == 0) {
			// 无记录则显示“无”
			ViewGroup parent = (ViewGroup) getParent();
//			parent.removeAllViews();
//			parent.addView(LayoutInflater.from(getContext()).inflate(R.layout.item_list_text, null));
		// horizonal的老师列表为空
		} else if (adapter.getCount() == 1 && adapter.getItemViewType(0) == TYPE_HORIZONTAL_SCROLL_TEACHERS) {
			Object params = adapter.getDatas().get(0);
			if (params instanceof ArrayList<?>) {
				ArrayList<DMTeacher> teachers = (ArrayList<DMTeacher>) params;
				if (teachers == null || teachers.size() == 0) {
					// 无记录则显示“无”
					addView(LayoutInflater.from(getContext()).inflate(R.layout.item_list_text_no_padding, null));
				}
			}
		}
		super.setAdapter(adapter);
	}

	public class YxListAdapter extends BaseAdapter {
		
		private ArrayList<Integer> mItemTypes;
		private ArrayList<Object> mData;
		
		public ArrayList<Object> getDatas() {
			return mData;
		}
		
		public void setData(ArrayList<Integer> itemTypes, ArrayList<Object> data) {
			mItemTypes = itemTypes;
			mData = data;
			// for Click
			//
			notifyDataSetChanged();
		}
		
		@Override
		public int getViewTypeCount() {
			return TYPE_COUNT;
		}
		
		@Override
		public int getItemViewType(int position) {
			return mItemTypes.get(position);
		}

		@Override
		public int getCount() {
			return mData == null ? 0 : mData.size();
		}
		
		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			View ret = null;
			if (getItemViewType(arg0) == TYPE_TEXT) {
				if (arg1 == null) {
					ret = LayoutInflater.from(getContext()).inflate(R.layout.item_list_text, null);
				} else {
					ret = arg1;
				}
				// customize description
				if (mData.get(arg0) instanceof String) {
					UIHelper.setText(ret, R.id.desc, (String) mData.get(arg0));
				}
				
			} else if (getItemViewType(arg0) == TYPE_IMG) {
				if (arg1 == null) {
					ret = LayoutInflater.from(getContext()).inflate(R.layout.item_list_image, null);
				} else {
					ret = arg1;
				}
				// customize images
				if (mData.get(arg0) instanceof String) {
					UIHelper.asyncLoadRoundImage(
							(ImageView) ret.findViewById(R.id.imageView1),
							(String) mData.get(arg0), R.drawable.default_banner);
				}
			} else if (getItemViewType(arg0) == TYPE_TEACHER_ITEM) {
				if (arg1 == null) {
					ret = LayoutInflater.from(getContext()).inflate(R.layout.item_list_teacher, null);
				} else {
					ret = arg1;
				}
				if (mData.get(arg0) instanceof DMTeacher) {
					DMTeacher teacher = (DMTeacher) mData.get(arg0);
					UIHelper.setText(ret, R.id.teacher_name, teacher.getTeacherName());
					UIHelper.asyncLoadImage((ImageView) ret.findViewById(R.id.teacher_img), teacher.getIconUrl(), R.drawable.default_icon);
				}
			} else if (getItemViewType(arg0) == TYPE_HORIZONTAL_SCROLL_TEACHERS) {
				ArrayList<DMTeacher> teachersList = null;
				if (mData.get(arg0) instanceof ArrayList<?>) {
					teachersList = (ArrayList<DMTeacher>) mData.get(arg0);
				} else {
					ret = arg1;
				}
				YxTearchersHSVHelper helper = new YxTearchersHSVHelper(getContext());
				ret = helper.getView();
				HsvAdapter helpAdapter = helper.new HsvAdapter(teachersList);
				helper.setAdapter(helpAdapter);
			}
			return ret;
		}

		@Override
		public Object getItem(int arg0) {
			return arg0;
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}
	}
	
}
